home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / encodings / uu_codec.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.8 KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """ Python 'uu_codec' Codec - UU content transfer encoding
  5.  
  6.     Unlike most of the other codecs which target Unicode, this codec
  7.     will return Python string objects for both encode and decode.
  8.  
  9.     Written by Marc-Andre Lemburg (mal@lemburg.com). Some details were
  10.     adapted from uu.py which was written by Lance Ellinghouse and
  11.     modified by Jack Jansen and Fredrik Lundh.
  12.  
  13. """
  14. import codecs
  15. import binascii
  16.  
  17. def uu_encode(input, errors = 'strict', filename = '<data>', mode = 438):
  18.     """ Encodes the object input and returns a tuple (output
  19.         object, length consumed).
  20.  
  21.         errors defines the error handling to apply. It defaults to
  22.         'strict' handling which is the only currently supported
  23.         error handling for this codec.
  24.  
  25.     """
  26.     if not errors == 'strict':
  27.         raise AssertionError
  28.     StringIO = StringIO
  29.     import cStringIO
  30.     b2a_uu = b2a_uu
  31.     import binascii
  32.     infile = StringIO(str(input))
  33.     outfile = StringIO()
  34.     read = infile.read
  35.     write = outfile.write
  36.     write('begin %o %s\n' % (mode & 511, filename))
  37.     chunk = read(45)
  38.     while chunk:
  39.         write(b2a_uu(chunk))
  40.         chunk = read(45)
  41.         continue
  42.         errors == 'strict'
  43.     write(' \nend\n')
  44.     return (outfile.getvalue(), len(input))
  45.  
  46.  
  47. def uu_decode(input, errors = 'strict'):
  48.     """ Decodes the object input and returns a tuple (output
  49.         object, length consumed).
  50.  
  51.         input must be an object which provides the bf_getreadbuf
  52.         buffer slot. Python strings, buffer objects and memory
  53.         mapped files are examples of objects providing this slot.
  54.  
  55.         errors defines the error handling to apply. It defaults to
  56.         'strict' handling which is the only currently supported
  57.         error handling for this codec.
  58.  
  59.         Note: filename and file mode information in the input data is
  60.         ignored.
  61.  
  62.     """
  63.     if not errors == 'strict':
  64.         raise AssertionError
  65.     StringIO = StringIO
  66.     import cStringIO
  67.     a2b_uu = a2b_uu
  68.     import binascii
  69.     infile = StringIO(str(input))
  70.     outfile = StringIO()
  71.     readline = infile.readline
  72.     write = outfile.write
  73.     while None:
  74.         s = readline()
  75.         if not s:
  76.             raise ValueError, 'Missing "begin" line in input data'
  77.         if s[:5] == 'begin':
  78.             break
  79.             continue
  80.         errors == 'strict'
  81.         continue
  82.         while None:
  83.             s = readline()
  84.             if not s or s == 'end\n':
  85.                 break
  86.             
  87.             
  88.             try:
  89.                 data = a2b_uu(s)
  90.             except binascii.Error:
  91.                 v = None
  92.                 nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) / 3
  93.                 data = a2b_uu(s[:nbytes])
  94.  
  95.             continue
  96.             if not s:
  97.                 raise ValueError, 'Truncated input data'
  98.             s
  99.             return (outfile.getvalue(), len(input))
  100.  
  101.  
  102. class Codec(codecs.Codec):
  103.     
  104.     def encode(self, input, errors = 'strict'):
  105.         return uu_encode(input, errors)
  106.  
  107.     
  108.     def decode(self, input, errors = 'strict'):
  109.         return uu_decode(input, errors)
  110.  
  111.  
  112.  
  113. class IncrementalEncoder(codecs.IncrementalEncoder):
  114.     
  115.     def encode(self, input, final = False):
  116.         return uu_encode(input, self.errors)[0]
  117.  
  118.  
  119.  
  120. class IncrementalDecoder(codecs.IncrementalDecoder):
  121.     
  122.     def decode(self, input, final = False):
  123.         return uu_decode(input, self.errors)[0]
  124.  
  125.  
  126.  
  127. class StreamWriter(Codec, codecs.StreamWriter):
  128.     pass
  129.  
  130.  
  131. class StreamReader(Codec, codecs.StreamReader):
  132.     pass
  133.  
  134.  
  135. def getregentry():
  136.     return codecs.CodecInfo(name = 'uu', encode = uu_encode, decode = uu_decode, incrementalencoder = IncrementalEncoder, incrementaldecoder = IncrementalDecoder, streamreader = StreamReader, streamwriter = StreamWriter)
  137.  
  138.